//
//  FindTableViewController.m
//  Car
//
//  Created by lanou on 15/9/17.
//  Copyright (c) 2015年 朱鸿儒. All rights reserved.
//

#import "FindTableViewController.h"
#import "HotCarTableViewCell.h"
#import "AllCarTableViewCell.h"
#import "BrandModel.h"

@interface FindTableViewController ()
@property (nonatomic ,strong)NSMutableArray *array;

@property (nonatomic,strong)NSMutableArray *allArray;

@property (nonatomic,strong)UIActivityIndicatorView *flowerView;

@end

@implementation FindTableViewController

- (void)viewDidLoad {
    self.flowerView = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    [self.tableView addSubview:self.flowerView];
    [self.flowerView startAnimating];
    
    [super viewDidLoad];
    [self setUpData];
    
    [self reloadAndJosn];
    
}
- (void)setUpData{
   
    self.tableView.separatorStyle = 0;
    [self.tableView registerClass:[HotCarTableViewCell class] forCellReuseIdentifier:@"hotCell"];
    [self.tableView registerClass:[AllCarTableViewCell class] forCellReuseIdentifier:@"tableViewCell"];
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemSearch) target:self action:@selector(searchButtonClick:)];
    
    self.navigationItem.rightBarButtonItem = rightButton;
    
    UIBarButtonItem *aButton = [[UIBarButtonItem alloc]initWithTitle:@"品牌" style:(UIBarButtonItemStyleDone) target:self action:@selector(aButtonClick:)];
    UIBarButtonItem *bButton = [[UIBarButtonItem alloc]initWithTitle:@"筛选" style:(UIBarButtonItemStyleDone) target:self action:@selector(bButtonClick:)];
    UIBarButtonItem *cButton = [[UIBarButtonItem alloc]initWithTitle:@"降价" style:(UIBarButtonItemStyleDone) target:self action:@selector(cButtonClick:)];
    UIBarButtonItem *dButton = [[UIBarButtonItem alloc]initWithTitle:@"二手车" style:(UIBarButtonItemStyleDone) target:self action:@selector(dButtonClick:)];
    NSArray *buttonArray = [[NSArray alloc]initWithObjects:aButton,bButton,cButton,dButton, nil];
    self.navigationItem.leftBarButtonItems = buttonArray;

    //把导航条设置不透明
    self.navigationController.navigationBar.translucent = NO;
   
    
}
#pragma mark--reloadAndJosn
- (void)reloadAndJosn{
    
    [LORequestManger GET:kBrand success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        for (NSDictionary *Dic in dic[@"result"][@"brandlist"]) {
            NSMutableArray *array = [[NSMutableArray alloc]init];
            NSArray *arr = Dic[@"list"];
            NSString *str = Dic[@"letter"];
            [self.array addObject:str];
            for (NSDictionary *brand in arr) {
                
                BrandModel *model = [BrandModel shareJosnWithDic:brand];
                model.letter = str;
                [array addObject:model];
                
            }
            [self.allArray addObject:array];
        }
        [self.tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}
#pragma mark--懒加载
- (NSMutableArray *)array{
    if (!_array) {
        _array = [[NSMutableArray alloc]init];
    }
    return _array;
}
- (NSMutableArray *)allArray{
    if (!_allArray) {
        _allArray = [[NSMutableArray alloc]init];
    }
    return _allArray;
}

- (void)aButtonClick:(UIBarButtonItem *)button{
    FindTableViewController *findVC = [[FindTableViewController alloc]init];
    SaleTableViewController *saleVC = [[SaleTableViewController alloc]init];
    IIViewDeckController *deckVC = [[IIViewDeckController alloc]initWithCenterViewController:findVC leftViewController:saleVC];
    deckVC.leftSize = 75;
    
    [self.navigationController pushViewController:deckVC animated:NO];
   
    
}
- (void)bButtonClick:(UIBarButtonItem *)button{
    ScreenTableViewController *screenVC = [[ScreenTableViewController alloc]init];
    
    [self.navigationController pushViewController:screenVC animated:NO];
}
- (void)cButtonClick:(UIBarButtonItem *)button{
    MarkDownTableViewController *markDownVC = [[MarkDownTableViewController alloc]init];
    [self.navigationController pushViewController:markDownVC animated:NO];
}
- (void)dButtonClick:(UIBarButtonItem *)button{
    UsedCarTableViewController *usedCarVC = [[UsedCarTableViewController alloc]init];
    [self.navigationController pushViewController:usedCarVC animated:NO];
}

- (void)searchButtonClick:(UIBarButtonItem *)rightButton{
    SearchTableViewController *searchVC = [[SearchTableViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:YES];
}


#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.allArray.count ;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.allArray[section] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        AllCarTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableViewCell" forIndexPath:indexPath];
        [cell setValueWithModel:self.allArray[indexPath.section][indexPath.row]];
        return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [self.allArray[section][0] letter];

}

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return self.array;
}

#pragma mark--点击cell跳转
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"id" object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[self.allArray[indexPath.section][indexPath.row] ID],@"key", nil]];
    [self.viewDeckController openLeftViewAnimated:YES];

}


@end
